package com.iams.manage.controller;

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.iams.common.annotation.Log;
import com.iams.common.core.controller.BaseController;
import com.iams.common.core.domain.AjaxResult;
import com.iams.common.enums.BusinessType;
import com.iams.manage.domain.Cabinet;
import com.iams.manage.service.ICabinetService;
import com.iams.common.utils.poi.ExcelUtil;
import com.iams.common.core.page.TableDataInfo;

/**
 * 档案柜管理Controller
 * 
 * @author zhjm
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/manage/cabinet")
public class CabinetController extends BaseController
{
    @Autowired
    private ICabinetService cabinetService;

    /**
     * 查询档案柜管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:cabinet:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cabinet cabinet)
    {
        startPage();
        List<Cabinet> list = cabinetService.selectCabinetList(cabinet);
        return getDataTable(list);
    }

    /**
     * 导出档案柜管理列表
     */
    @PreAuthorize("@ss.hasPermi('manage:cabinet:export')")
    @Log(title = "档案柜管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cabinet cabinet)
    {
        List<Cabinet> list = cabinetService.selectCabinetList(cabinet);
        ExcelUtil<Cabinet> util = new ExcelUtil<Cabinet>(Cabinet.class);
        util.exportExcel(response, list, "档案柜管理数据");
    }

    /**
     * 获取档案柜管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:cabinet:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cabinetService.selectCabinetById(id));
    }

    /**
     * 新增档案柜管理
     */
    @PreAuthorize("@ss.hasPermi('manage:cabinet:add')")
    @Log(title = "档案柜管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cabinet cabinet)
    {
        return toAjax(cabinetService.insertCabinet(cabinet));
    }

    /**
     * 修改档案柜管理
     */
    @PreAuthorize("@ss.hasPermi('manage:cabinet:edit')")
    @Log(title = "档案柜管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cabinet cabinet)
    {
        return toAjax(cabinetService.updateCabinet(cabinet));
    }

    /**
     * 删除档案柜管理
     */
    @PreAuthorize("@ss.hasPermi('manage:cabinet:remove')")
    @Log(title = "档案柜管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cabinetService.deleteCabinetByIds(ids));
    }

    /**
     * 更新档案柜尺寸
     */
    @PreAuthorize("@ss.hasPermi('manage:cabinet:update')")
    @Log(title = "档案柜管理", businessType = BusinessType.UPDATE)
    @PutMapping("/size")
    public AjaxResult updateSize(@RequestParam String location, @RequestParam String option)
    {
        try {
            // 解析字符串
            String[] parts = location.split("号库房第|排第|列档案柜第|节第|层");
            if (parts.length != 5) {
                return error("Invalid location format");
            }

            Long repositoryId = Long.parseLong(parts[0]);
            Integer pai = Integer.parseInt(parts[1]);
            Integer lie = Integer.parseInt(parts[2]);

            // 查询档案柜
            Cabinet cabinet = cabinetService.selectCabinetByRepositoryAndLocation(repositoryId, pai, lie);
            if (cabinet == null) {
                return error("Cabinet not found");
            }

            // 更新 size 字段
            if (option.equals("add")) {
                cabinet.setSize(cabinet.getSize() + 1);
            } else if (option.equals("delete")) {
                cabinet.setSize(cabinet.getSize() - 1);
            } else {
                return error("Invalid option");
            }
            int result = cabinetService.updateCabinet(cabinet);
            if (result > 0) {
                return success();
            } else {
                return error("Failed to update cabinet size");
            }
        } catch (Exception e) {
            return error("Error updating cabinet size: " + e.getMessage());
        }
    }
}
